package com.antwork.common.web.jsp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class TableConfig {
	private String[] head;
	private String[] field;
	private int[] textLimit;
	private String[] width;
	private String onclickField;
	private String onclickHref;
	private String opername;
	private String[][] opertype;
	private HashMap<String, FieldExpand> fieldexpand;
	private HashMap<String, String> fieldpopover;

	public String[] getHead() {
		return this.head;
	}

	public void setHead(String[] head) {
		this.head = head;
	}

	public String[] getField() {
		return this.field;
	}

	public void setField(String[] field) {
		this.field = field;
	}

	public int[] getTextLimit() {
		return this.textLimit;
	}

	public void setTextLimit(int[] textLimit) {
		this.textLimit = textLimit;
	}

	public String[] getWidth() {
		return this.width;
	}

	public void setWidth(String[] width) {
		this.width = width;
	}

	public String getOnclickField() {
		return this.onclickField;
	}

	public void setOnclickField(String onclickField) {
		this.onclickField = onclickField;
	}

	public String getOnclickHref() {
		return this.onclickHref;
	}

	public void setOnclickHref(String onclickHref) {
		this.onclickHref = onclickHref;
	}

	public String getOpername() {
		return this.opername;
	}

	public void setOpername(String opername) {
		this.opername = opername;
	}

	public String[][] getOpertype() {
		return this.opertype;
	}

	public void setOpertype(String[][] opertype) {
		this.opertype = opertype;
	}

	public void setFieldexpand(String fieldName, String expandStr) {
		if (this.fieldexpand == null) {
			this.fieldexpand = new HashMap();
		}
		String type = expandStr.substring(0, expandStr.indexOf("("));
		String str = expandStr.substring(expandStr.indexOf("(") + 1,
				expandStr.indexOf(")"));
		if (type.equals("long2date")) {
			Long2Date date = new Long2Date();
			date.format = str;
			this.fieldexpand.put(fieldName, date);
		} else if (type.equals("decode")) {
			Decode decode = new Decode();
			decode.map = new HashMap();
			String[] s = str.split("&");
			for (int i = 0; i < s.length; ++i) {
				String[] kv = s[i].split(":");
				decode.map.put(kv[0], kv[1]);
			}
			this.fieldexpand.put(fieldName, decode);
		}
	}

	public String getFieldExpand(String fieldName, String fieldValue) {
		if (this.fieldexpand == null) {
			return fieldValue;
		}
		FieldExpand fieldExpand = (FieldExpand) this.fieldexpand.get(fieldName);
		if (fieldExpand == null) {
			return fieldValue;
		}
		if (fieldExpand instanceof Long2Date) {
			Long2Date ld = (Long2Date) fieldExpand;
			Date date = new Date(Long.parseLong(fieldValue) * 1000L);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ld.format);
			return simpleDateFormat.format(date);
		}
		if (fieldExpand instanceof Decode) {
			Decode d = (Decode) fieldExpand;
			String dv = (String) d.map.get(fieldValue);
			if (dv == null) {
				dv = (String) d.map.get("#");
			}
			return dv;
		}
		return fieldValue;
	}

	public String getFieldpopover(String field) {
		if (this.fieldpopover == null) {
			return null;
		}
		return (String) this.fieldpopover.get(field);
	}

	public void putFieldpopover(String field, String popoverStr) {
		if (this.fieldpopover == null) {
			this.fieldpopover = new HashMap();
		}
		this.fieldpopover.put(field, popoverStr);
	}

	class Decode extends TableConfig.FieldExpand {
		HashMap<String, String> map;

		Decode() {
			// super(TableConfig.this);
		}
	}

	class FieldExpand {
		FieldExpand() {
		}
	}

	class Long2Date extends TableConfig.FieldExpand {
		String format;

		Long2Date() {
			// super(TableConfig.this);
		}
	}

	class Operation {
		String fieldName;
		String fieldHref;

		Operation() {
		}
	}
}